package com.mstore.service;

import java.util.ArrayList;
import java.util.List;

import com.mstore.dao.OrderInfoDao;
import com.mstore.pojo.OrderInfo;
import com.mstore.util.CreateLaundry;

public class OrderInfoService {

	private OrderInfoDao orderInfoDao;

	/**
	 * 查询企业的订单
	 */
	public List<OrderInfo> watchMyOrderInfo(Long eaId) {
		List<OrderInfo> orderInfos = new ArrayList<OrderInfo>();
		List<Object> objects = orderInfoDao.watchMyOrderInfo(eaId);
		for (Object object : objects) {
			orderInfos.add((OrderInfo) object);
		}
		return orderInfos;
	}

	/**
	 * 企业下订单
	 */
	public void addMyOrderInfo(OrderInfo orderInfo) {
		// 生成订单编号
		orderInfo.setOrderId(new CreateLaundry().createLaundryCode());
		orderInfoDao.addMyOrderInfo(orderInfo);
	}
	
	/**
	 * 通过流水号查询订单
	 */
	public OrderInfo finadMyOrderInfoByOrderId(String orderId) {
		List<OrderInfo> orderInfos = new ArrayList<OrderInfo>();
		List<Object> objects = orderInfoDao.finadMyOrderInfoByOrderId(orderId);
		for (Object object : objects) {
			orderInfos.add((OrderInfo) object);
		}
		if (orderInfos != null && orderInfos.size() > 0) {
			return orderInfos.get(0);
		}
		return null;
	}
	
	/**
	 * 更新企业订单
	 */
	public void updateMyOrderInfo(OrderInfo orderInfo) {
		orderInfoDao.updateMyOrderInfo(orderInfo);
	}
	
	/**
	 * 根据企业id和资费表id查询订单
	 * 返回最新的“已支付”状态的订单
	 */
	public OrderInfo findMyOrderInfoByEaIdAndTariffId(Long eaId, Long tariffId) {
		Object obj = orderInfoDao.findMyOrderInfoByEaIdAndTariffId(eaId, tariffId);
		return obj == null ? null : (OrderInfo) obj;
	}
	
	/**
	 * 删除订单
	 * * @param orderInfo
	 */
	public void delOrderInfo(OrderInfo orderInfo) {
		orderInfoDao.delOrderInfo(orderInfo);
	}

	public OrderInfoDao getOrderInfoDao() {
		return orderInfoDao;
	}

	public void setOrderInfoDao(OrderInfoDao orderInfoDao) {
		this.orderInfoDao = orderInfoDao;
	}

}
